package cn.wang.controller;

import cn.wang.common.vo.PageResult;
import cn.wang.pojo.Brand;
import cn.wang.pojo.dto.QueryBrandDTO;
import cn.wang.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    IBrandService brandService;

    @GetMapping("/list")
    public ResponseEntity<PageResult<Brand>> queryPage(QueryBrandDTO queryBrandDTO){
        return ResponseEntity.ok(brandService.queryPage(queryBrandDTO));
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam(name = "cids")List<Long> cids){
        brandService.save(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand,@RequestParam(name = "cids")List<Long> cids){
        brandService.update(brand,cids);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id")Long id){
        brandService.deleteById(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 根据商品分类查询品牌
     * @param cid
     * @return
     */
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandsByCid(@PathVariable("cid")Long cid){
        return ResponseEntity.ok(brandService.queryBrandsByCid(cid));
    }

    @GetMapping("/bid")
    public ResponseEntity<Brand> queryBrandByBid(@RequestParam("bid")Long bid){
        return ResponseEntity.ok(brandService.queryBrandByBid(bid));
    }

    @GetMapping("/brands")
    ResponseEntity<List<Brand>> queryBrandsByBIds(@RequestParam("bids")Long... bids){
        return ResponseEntity.ok(brandService.queryBrandsByBIds(bids));
    }
}
